<div id="myForm"></div>
<div id="btn-add">
	<button class="byn btn-primary btn-small" onclick="addAcc()">再添一个</button>
</div>
<div id="accObj"></div>
<style>
#myForm {
	position: absolute;
	top: 70px;
	bottom: 0;
	left: 20px;
	width: 310px;
	/*border: 1px solid red;*/
}

#btn-add {
	position: absolute;
	top: 70px;
	left: 350px;
}

#accObj {
	position: absolute;
	top: 100px;
	/*bottom: 0;*/
	left: 350px;
	/*right: 0;*/
	width: 800px;
}
</style>
<script>
var	formStructure = [
   	          	{type:"fieldset",label:"二级发放清单",inputWidth:"auto",list:[
   	          		{type:"settings",labelWidth:80,inputWidth:170},
   	          		{type:"block",inputWidth:"auto",blockOffset:0,offsetTop:0,list:[
   	          			{"type":"input","name":"form_type","label":"表单分类",value:"{$Think.session.user['role']}发放",readonly:true},
   	          			{"type":"input","name":"username","label":"建单人",value:"{$Think.session.user['name']}",readonly:true},
   	          			{"type":"input","name":"usertel","label":"建单人电话",value:"{$Think.session.user['tel']}"},
   	          			{"type":"combo","name":"location","label":"存放地点",required: true},
   	          			{"type":"input","name":"owner","label":"使用人",value:"{$Think.session.user['name']}",required: true},
   	          			//{"type":"input","name":"uusage","label":"用途",rows:5,required: true},
   	          			{"type":"input","name":"dept","label":"使用单位",value:"{$Think.session.user['dept']}",note:{text:"若不正确请在个人设置里修改"},readonly:true},
   	          			//{"type":"input","name":"progress","label":"审批状态",value:"待审批",disabled:true},
   	          			{"type":"input","name":"mremarks","label":"备注",rows:3,note:{text:"选填"}},
   	          		]},
   	          		{type:"block",inputWidth:"250",blockOffset:"80",offsetTop:20,list:[
   	          			{"type":"button","name":"f_submit","value":"提交"},
   	          		]}
   	          	]}
   	          ];
var myForm = new dhtmlXForm('myForm',formStructure);
$.post("get_combo_options.html",{
	table:"store",field:{text:"sname",value:"sname"},distinct:1,where:{slevel:2}
	},function(data){
		myForm.getCombo("location").addOption(data);
});
myForm.attachEvent("onButtonClick", function(id){
	if(id=="f_submit"){
		/*myForm.send("{$_SERVER["REQUEST_URI"]}",function(loader,response){
			//对返回值的处理
			console.log(response);
		});*/
		sendApply();
	} else if (id=='ttt'){
		myForm.loadStruct({data:{"class":"备件","name":"","count":"55","location":"","usage":"uuuuuu","username":"","progress":"","remarks":"","form_type":"领用申请","create_time":"","type":"","marker":"sdf e","units":"","dept":"","usertel":""}});
	} else if (id=='submit'){
		
	} else {
    	alert("Button with name "+id+" was clicked");
    }
});
myForm.enableLiveValidation(true);

var myAcc = new dhtmlXAccordion({
	parent: "accObj",
	multi_mode: true,
});
var id = 0,idd;
var accForm = [];
addAcc();
function sendApply(){
	if(!myForm.validate()){
		dhtmlx.alert({title:"信息不全",type:"alert-warning",text:"将领用申请单填写完整,并重试"});
		return false;
	}
	var data = {
		"apply": [myForm.getFormData()]
	};
	for (var i = 1; i < accForm.length ;i++){
		if( !accForm[i].validate() ){
			dhtmlx.alert({title:"信息不全",type:"alert-warning",text:"将领用明细填写完整再提交"});
			return false;
		}
		var item_id = accForm[i].getCombo("maker"+i).getSelectedValue();
		if( item_id == undefined ){
			dhtmlx.alert({title:"请选择正确的制造厂商",type:"alert-warning",text:"无法获取领用物品"+i+"的id,无法提交"});
			return false;
		} else {
			data['apply'][i] = {};
			data['apply'][i]['item_id'] = item_id;
			data['apply'][i]['count'] = accForm[i].getItemValue("count"+i);
			data['apply'][i]['dusage'] = accForm[i].getItemValue("dusage"+i);
			data['apply'][i]['dremarks'] = accForm[i].getItemValue("dremarks"+i);
		}
	}
	data['apply'][0]['formAccount'] = id;
	$.post("{$_SERVER["REQUEST_URI"]}",data,function(data){
		if(data.state)
			data.callback = function(){
				location.href = "history.html";
			};
		dhtmlx.alert(data);
	});
	data = null;
}
function addAcc(){
	id ++;
	myAcc.addItem("a"+id,"发放物品"+id,true,165);
	{include file="C/user_apply_addStructure" /} // include 
	accForm[id] = myAcc.cells("a"+id).attachForm(addStructure);
	accForm[id].enableLiveValidation(true);
	$.post("get_combo_options.html",{
		table:"item",field:{text:"name",value:"units"},distinct:1
		},function(data){
			accForm[id].getCombo("name"+id).addOption(data);
	});
	
	//accForm[id].getCombo("name"+id).load("get_combo_options.html?table=item&text=name&value=name&distinct=1");
	accForm[id].getCombo("name"+id).enableFilteringMode("between");//开启模糊查询
	accForm[id].getCombo("type"+id).enableFilteringMode("between");
	// 表单事件监听
	var idd = id;	//保存当前的 id 值, 在 callback 函数中使用
	accForm[id].getCombo("name"+id).attachEvent("onClose", function (value, text){
		//if(name.substr(0,4) == "name"){
			//var idd = name.substr(4, name.length); //保存当前的 id 值, 在 callback 函数中使用
			setUnitsValue(idd,accForm[idd].getCombo("name"+idd).getSelectedValue());
			accForm[idd].getCombo("type"+idd).setComboText("");
			accForm[idd].getCombo("maker"+idd).setComboText("");
			//accForm[idd].getCombo("type"+idd).detachEvent("onChange");
			accForm[idd].getCombo("type"+idd).attachEvent("onChange", function (value, text){
				accForm[idd].getCombo("maker"+idd).setComboText("");
				//accForm[idd].getCombo("maker"+idd).detachEvent("onChange");
				accForm[idd].getCombo("maker"+idd).attachEvent("onChange", function (value, text){
					
					//setUnitsValue(idd,value);
				});
				getMakerOptions(idd,text);
			});
			getTypeOptions(idd,accForm[idd].getCombo("name"+idd).getSelectedText());
		//}		
	});
	/*accForm[id].attachEvent("onAfterValidate", function (status){
		if(!status){
			$(".validate_error input").css("border","1px solid red");
		}
	});*/
}
/**
 * 获取 combo 的 options 
 *
 * idd: form 序号
 * table: 表名  
 * field: 获取的 option 的 field (json格式)
 * where： 查询的条件限定 (json格式 )
 */
function getOptions(idd,table,field,where){	
	$.post("get_combo_options.html",{
		table:table,field:field,distinct:1,where:where
		},function(options){
			//var options = data['options'];
			accForm[idd].getCombo(field['text']+idd).clearAll();
			accForm[idd].getCombo(field['text']+idd).addOption(options);
			if (options.length > 1){
				accForm[idd].setNote(field['text']+idd,{text:"有多个选项，请选择。"});
			} else {
				accForm[idd].setNote(field['text']+idd,{text:"唯一选项,没得选择。"});
				//options[0]['selected'] = true;//设置默认选择第一个
				accForm[idd].getCombo(field['text']+idd).selectOption(0,true,true);
			}
			$(".dhxform_note").width(170); // 修复 dhtmlxForm 里的 combo 设置 note width 为 0 的 bug  
			// JQ ajax callback 函数是动态执行的, 使用的 idd 为所属 Form 的 id 而不是最后一个 Form 的 id .
			option = null;
	});
}
function getTypeOptions(idd,name){
	getOptions(idd,"item",{text:"type",value:"type"},{name:name});
}
function getMakerOptions(idd,type){
	getOptions(idd,"item",{text:"maker",value:"id"},{name:accForm[idd].getCombo("name"+idd).getSelectedText(),type:type});
	// 隐含 value 值 为 id
}
function setUnitsValue(idd,value){
	accForm[idd].setItemValue("units"+idd,value);
}
$(function($){
	$(".dhxacc_cont").height($(".dhxacc_cont").height()-20); //拯救 dhtmlx Layout 组件 导致 DOM 溢出出现滚动条
});
</script>